package com.example.springsecurity.controller;

import com.example.springsecurity.pojo.SysUser;
import com.example.springsecurity.service.SysUserService;
import com.example.springsecurity.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {
 
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
    @Autowired
    private AuthenticationManager authenticationManager;

    @ResponseBody
    @PreAuthorize("hasAuthority('system:dept:list')")
    @RequestMapping("hi")
    String hi(){
        return "需要权限才能访问";
    }



    @GetMapping("/list")
    @ResponseBody
    @PreAuthorize("hasAuthority('system:dept:list')")
    public String list(){
        return "列表功能";
    }

    @GetMapping("/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('system:test:list')")
    public String delete(){
        return "删除功能";
    }

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/doLogin")
    public ResponseResult login( SysUser sysUser){

        String token = sysUserService.login(sysUser);

        HashMap<String,String> map = new HashMap<>();
        map.put("token",token);
        ResponseResult result = new ResponseResult<Map>(HttpStatus.OK.value(),"登录成功",map);
        return result;
    }
    @PostMapping("/doout")
    public ResponseResult doout(){
        sysUserService.logout();
        ResponseResult result = new ResponseResult(HttpStatus.OK.value(),"登出成功");
        return result;
    }
}